import generatedRoutes from "virtual:generated-pages";
import { RouteRecordRaw } from "vue-router";

const parentRouters = [
  { title: "财务中心", isParent: true, icon: "Money" },
];
console.log(generatedRoutes, "generatedRoutes");

const routers: RouteRecordRaw[] = [];
parentRouters.forEach((parent) => {
  if (parent.isParent) {
    const children = generatedRoutes
      .filter((chi) => chi.meta?.parent === parent.title)
      .sort((a, b) => {
        !a.meta && (a.meta = { index: 9999 });
        !b.meta && (b.meta = { index: 9999 });
        return (a.meta.index as number) - (b.meta.index as number);
      });
    if (children.length > 0) {
      const name = (children[0].name as string).split("-")[0];
      routers.push({
        name: name,
        path: `/${name}`,
        meta: {
          title: parent.title,
          icon: parent.icon,
        },
        children,
      });
    }
  } else {
    const routes = generatedRoutes.filter(
      (r) => r.meta?.title === parent.title
    );
    console.log(routes, "routesroutes");

    if (routes.length > 0) {
      routers.push(...routes);
    }
  }
});
export default routers;
